package com.Monica.controller;

import com.Monica.pojo.Message;
import com.Monica.pojo.User;
import com.Monica.service.UserService;
import com.Monica.service.UserServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "LoginServlet")
public class LoginServlet extends HttpServlet {
    private UserService userService = new UserServiceImp();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = new User(username,password);
        Message message = userService.login(user);
        if (message.isSuccess()){
            request.getSession().setAttribute("user",user);
            response.getWriter().println("<script>alert('"+message.getMsg()+"');window.location.href='success.jsp'</script>");
        }else {
            response.getWriter().println("<script>alert('"+message.getMsg()+"');window.location.href='input.jsp'</script>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
